package app.ep1.seminarios;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import app.ep1.R;

public class ListaAlunosSeminarioActivity extends Activity {
	private static int REQUEST_CODE = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lista_alunos_seminario);

		try {
			Bundle bundle = this.getIntent().getExtras();
			TextView textView = (TextView) findViewById(R.id.textViewNomeSeminario);
			textView.setText(bundle.getString("nomeSeminario"));
		} catch (Exception e) {
		}

		Button adicionaAluno = (Button) findViewById(R.id.buttonAdicionarAluno);
		adicionaAluno.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent i = new Intent(v.getContext(), BarcodeActivity.class);
				startActivityForResult(i, REQUEST_CODE);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode != REQUEST_CODE)
			return;

		if (resultCode != RESULT_OK)
			return;

		Bundle bundle = data.getExtras();
		String nusp = bundle.getString("nusp");

		adicionaLinhaDeAluno(nusp);
	}

	public void adicionaLinhaDeAluno(String nusp) {
		final TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayoutAlunosSeminario);

		final TableRow tableRow = new TableRow(this);
		tableRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));

		TextView textView = new TextView(this);
		textView.setText(nusp);

		Button button = new Button(this);
		button.setText("Apagar");
		button.setHeight(20);
		button.setWidth(150);

		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				tableLayout.removeView(tableRow);
			}
		});

		tableRow.addView(textView);
		tableRow.addView(button);
		tableLayout.addView(tableRow);
	}
}
